package com.jxwifi.cloud.quickclean.utils;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

public class DateTimeController extends BaseWheelController {

    private int mStartTime;//服务下单开始时间
    private int mTimeInterval;//时间间隔 如果是1表示间隔1小时，如果是2表示间隔2小时
    private int mEndTime;//服务下单结束时间

    private boolean mIsNewTime=true;

    public DateTimeController(int startTime, int timeinterval, int endtime) {
        super();
        this.mStartTime = startTime;
        this.mTimeInterval = timeinterval;
        this.mEndTime = endtime;
        getItems1();
        getItems2();
        getItems3();
        onPreExecute();
    }

    @Override
    public void setScrollLinked(boolean linked) {
        super.setScrollLinked(linked);
    }


    @Override
    public int[] getCurrentValues() {
        return super.getCurrentValues();
    }

    @Override
    public List getItems1() {
        List<String> mYears = new ArrayList<String>();
        SimpleDateFormat sdfday = new SimpleDateFormat("yyyy-MM-dd");
        Calendar now = Calendar.getInstance();
        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
        String date = sdf.format(new Date());
        String[] mNowTime = date.split(":");
        if(Integer.valueOf(mNowTime[0])<=(mEndTime-mTimeInterval)){
            int mHous=Integer.valueOf(mNowTime[0])+mTimeInterval;
            int mHousnow=mEndTime-mTimeInterval;
            if(mHous==mHousnow){
                if (Integer.valueOf(mNowTime[1])+5>=59){
                    mIsNewTime=false;
                }else{
                    now.setTime(new Date());
                    now.set(Calendar.DATE, now.get(Calendar.DATE));
                    mYears.add(sdfday.format(now.getTime()));
                    mIsNewTime=true;
                }

            }else{
                if (mHous>mHousnow){
                    mIsNewTime=false;
                }else{
                    now.setTime(new Date());
                    now.set(Calendar.DATE, now.get(Calendar.DATE));
                    mYears.add(sdfday.format(now.getTime()));
                    mIsNewTime=true;
                }
            }
        }else{
            mIsNewTime=false;
        }
        for (int i = 1; i < 15; i++) {
            now.set(Calendar.DATE, now.get(Calendar.DATE) + 1);
            mYears.add(sdfday.format(now.getTime()));
        }
        return mYears;
    }

    @Override
    public List getItems2() {
        List<String> mHour = new ArrayList<String>();
        int hourtime;
        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
        String date = sdf.format(new Date());
        String[] mNowTime = date.split(":");
        if (Integer.valueOf(mNowTime[0]) < mEndTime - mTimeInterval&& Integer.valueOf(mNowTime[0]) > mStartTime
                ) { //这里表示当前时间小于18点得时候显示信息

            if (Integer.valueOf(mNowTime[0])+mTimeInterval==mEndTime-mTimeInterval&&Integer.valueOf(mNowTime[1]) >= 55){
                for (int i = mStartTime; i <= mEndTime; i++) {
                    mHour.add(i + "时");
                }
            }else{
                if (Integer.valueOf(mNowTime[1]) >= 55) {
                    hourtime = Integer.valueOf(mNowTime[0]) + 1+mTimeInterval;
                } else {
                    hourtime = Integer.valueOf(mNowTime[0])+mTimeInterval;
                }
                if (Integer.valueOf(mNowTime[0])+mTimeInterval==mEndTime-mTimeInterval){
                    mHour.add(hourtime + "时");
                }else{
                    for (int i = hourtime ; i < mEndTime; i++) {
                        mHour.add(i + "时");
                    }
                }
            }
        } else {
            for (int i = mStartTime; i <= mEndTime; i++) {
                mHour.add(i + "时");
            }
        }
        return mHour;
    }

    @Override
    public List getItems3() {
        List<String> mMinute = new ArrayList<String>();
        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
        String date = sdf.format(new Date());
        String[] mNowTime = date.split(":");
        if (Integer.valueOf(mNowTime[1]) < 55&&mIsNewTime) { //获取小时集合中得第一个下标，表示当前得小时，这里显示得分钟为当前分钟每次添加5分钟
            int i = Integer.valueOf(mNowTime[1]);
            while (i < 55) {
                i = i + 5;
                if (i<10){
                    mMinute.add("0"+i + "分");
                }else{
                    mMinute.add(i + "分");
                }
            }
        } else {
            int k = 0;
            mMinute.add("0"+k + "分");
            while (k < 59) {
                k = k + 5;
                if (k!=60){
                    if (k<10){
                        mMinute.add("0"+k + "分");
                    }else{
                        mMinute.add(k + "分");
                    }
                }else{
                    k=k-1;
                    mMinute.add(k + "分");
                }
            }
        }
        return mMinute;
    }

    boolean mIstarget = true;

    @Override
    public void linkRefresh(List targetDatas, int scrollingWheel, int newValue) {
        if (scrollingWheel == 0) {
            //do nothing...
            if (newValue == 0) {
                mIstarget = true;
                List<String> mHour = new ArrayList<String>();
                int hourtime;
                SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
                String date = sdf.format(new Date());
                String[] mNowTime = date.split(":");
                if (Integer.valueOf(mNowTime[0]) < mEndTime - mTimeInterval && Integer.valueOf(mNowTime[0]) > mStartTime
                        ) { //这里表示当前时间小于22点得时候显示信息  &&Integer.valueOf(mNowTime[0])!=mEndTime-mTimeInterval
                    if (Integer.valueOf(mNowTime[0])+mTimeInterval==mEndTime-mTimeInterval&&Integer.valueOf(mNowTime[1]) >= 55){
                        for (int i = mStartTime; i <= mEndTime; i++) {
                            mHour.add(i + "时");
                        }
                    }else{
                        if (Integer.valueOf(mNowTime[1]) >= 55) {
                            hourtime = Integer.valueOf(mNowTime[0]) + 1+mTimeInterval;
                        } else {
                            hourtime = Integer.valueOf(mNowTime[0])+mTimeInterval;
                        }
                        if (Integer.valueOf(mNowTime[0])+mTimeInterval==mEndTime-mTimeInterval){
                            mHour.add(hourtime + "时");
                        }else{
                            for (int i = hourtime ; i < mEndTime; i++) {
                                mHour.add(i + "时");
                            }
                        }
                    }
                } else {
                    for (int i = mStartTime; i < mEndTime; i++) {
                        mHour.add(i + "时");
                    }
                }

                targetDatas.clear();
                targetDatas.addAll(mHour);
            } else {
                mIstarget = false;
                targetDatas.clear();
                List<String> mHour2 = new ArrayList<String>();
                for (int i = mStartTime; i < mEndTime; i++) {
                    mHour2.add(i + "时");
                }
                targetDatas.addAll(mHour2);
            }

        } else if (scrollingWheel == 1) {
            if (newValue == 0 && mIstarget) {
                List<String> mMinute = new ArrayList<String>();
                SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
                String date = sdf.format(new Date());
                String[] mNowTime = date.split(":");
                if (Integer.valueOf(mNowTime[1]) < 55&&mIsNewTime) { //获取小时集合中得第一个下标，表示当前得小时，这里显示得分钟为当前分钟每次添加5分钟
                    int i = Integer.valueOf(mNowTime[1]);
                    while (i < 55) {
                        i = i + 5;
                        if (i!=60){
                           // mMinute.add(i + "分");
                            if (i<10){
                                mMinute.add("0"+i + "分");
                            }else{
                                mMinute.add(i + "分");
                            }
                        }else{
                            i=i-1;
                            mMinute.add(i + "分");
                        }
                    }
                } else {
                    int k = 0;
                    while (k < 59) {
                        k = k + 5;
                        if (k!=60){
                           // mMinute.add(k + "分");
                            if (k<10){
                                mMinute.add("0"+k + "分");
                            }else{
                                mMinute.add(k + "分");
                            }
                        }else{
                            k=k-1;
                            mMinute.add(k + "分");
                        }
                    }
                }
                targetDatas.clear();
                targetDatas.addAll(mMinute);
            } else {
                targetDatas.clear();
                List<String> mMinute2 = new ArrayList<String>();
                int k = 0;
                while (k < 59) {
                    k = k + 5;
                    if (k!=60){
                        //mMinute2.add(k + "分");
                        if (k<10){
                            mMinute2.add("0"+k + "分");
                        }else{
                            mMinute2.add(k + "分");
                        }
                    }else{
                        k=k-1;
                        mMinute2.add(k + "分");
                    }
                }
                targetDatas.addAll(mMinute2);
            }
        }
    }
}
